﻿using System;
using System.Linq;
using JSC.Portal.Data;
using JSC.Portal.Entities;
using JSC.Core;
using JSC.Core.Data;
using JSC.Portal.Services;

namespace JSC.Portal.Data
{
    public class SectionService : CommonDao<Section>, ISectionService
    {
        #region ISectionService Members

        public Section GetSectionById(int id)
        {
            return GetById(id, false);
        }

        public void MoveSection(Section section, MoveAction moveAction)
        {
            switch (moveAction)
            {
                case MoveAction.MoveUp:
                    MoveUp(section);
                    break;
                case MoveAction.MoveDown:
                    MoveDown(section);
                    break;
                case MoveAction.MoveLeft:
                case MoveAction.MoveRight:
                default:
                    throw new NotSupportedException(moveAction.ToString());
            }
        }

        public bool CanMoveUp(Section section)
        {
            throw new NotImplementedException();
        }

        public bool CanMoveDown(Section section)
        {
            throw new NotImplementedException();
        }

        public Section GetNewSection(int portalPageId)
        {
            Section s = new Section();
            s.Page = FacadeFactory.CreateFacade<IPortalPageService>().GetById(portalPageId, false);
            SetPermissions(s.Page, s);
            return s;
        }
        #endregion

        private void SetPermissions(PortalPage parentPage, Section section)
        {
            foreach (PortalPagePermission ppp in parentPage.PagePermissions)
            {
                SectionPermission childPermission = new SectionPermission();
                childPermission.Section = section;
                childPermission.Role = ppp.Role;
                childPermission.ViewAllowed = ppp.ViewAllowed;
                childPermission.EditAllowed = ppp.EditAllowed;
                section.SectionPermissions.Add(childPermission);
            }
        }

        private void MoveDown(Section s)
        {
            if (s.Page != null)
            {
                foreach (Section section in s.Page.Sections)
                {
                    if (section.Placeholder == s.Placeholder && section.Position == s.Position + 1)
                    {
                        // switch positions
                        s.Position++;
                        section.Position--;
                        break;
                    }
                }
                s.Page.Sections.Remove(s);
                s.Page.Sections.Insert(s.Position, s);
            }
        }

        private void MoveUp(Section s)
        {
            if (s.Page != null)
            {
                foreach (Section section in s.Page.Sections)
                {
                    if (section.Placeholder == s.Placeholder && section.Position == s.Position - 1)
                    {
                        // switch positions
                        s.Position--;
                        section.Position++;
                        break;
                    }
                }
                s.Page.Sections.Remove(s);
                s.Page.Sections.Insert(s.Position, s);
            }
        }
    }
}
